package core;

import domain.Student;
import util.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author 于金鹏
 * @Date 2024/12/17 11:34
 * @Package StudentCheckSystem
 */
public class BaseHttpServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        super.service(req, resp);
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        res.setCharacterEncoding("utf-8");
        super.service(req, res);
    }

    // 执行业务
    public void executeBusiness(HttpServletResponse resp, ServiceBusinessExecute serviceBusinessExecute) throws IOException {
        try {
           serviceBusinessExecute.execute(resp);
        } catch (SQLException e) {
            e.printStackTrace();
            ResponseUtils.responseJSON(resp, AjaxResult.error(500, "数据库异常"));
        } catch (Exception e) {
            e.printStackTrace();
            ResponseUtils.responseJSON(resp, AjaxResult.error());
        }
    }
}
